package sueldos;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;

import com.swtdesigner.SWTResourceManager;

public class RenatreGUI {
	protected Shell shell;
	private Table table;
	

	/**
	 * Launch the application.
	 * @param args
	 */
	
	
	

	public void ObtenerDatos(String base){
		try {
			Connection conexion = DriverManager.getConnection("jdbc:mysql://localhost:3306/"+base, "", ""); 
			String query="SELECT DISTINCT legajo, CONCAT(apellido,', ',nombres) AS apenom FROM empleado " +
					"WHERE LEGAJO > 0 and fechaegr is null order by legajo"; //FECHAEGR IS NULL AND
			PreparedStatement ps = conexion.prepareStatement(query);
			ps.execute();
			ResultSet rs = ps.getResultSet();
			while (rs.next()){
				TableItem item = new TableItem(table, SWT.NONE);
				item.setText(0, rs.getString(1));
				item.setText(1, rs.getString(2));
			}
			ps.close();
			conexion.close();
			
			
		} catch (Exception e) {
			
		}

	}
	
	
	
	
	
	public static void main(String[] args) {
		Display.getDefault();
				try {
					RenatreGUI window = new RenatreGUI();
					window.open();
				} catch (Exception e) {
					e.printStackTrace();
				}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	
	
	
	
	
	protected void createContents() {
		shell = new Shell(SWT.CLOSE | SWT.MIN | SWT.MAX | SWT.SYSTEM_MODAL);
		shell.setImage(SWTResourceManager.getImage(RenatreGUI.class, "/imagenes/AppIcon.png"));
		shell.setSize(586, 447);
		shell.setText("Generador de Cupones Renatre");
		shell.setLayout(new FormLayout());
		
		Composite composite = new Composite(shell, SWT.NONE);
		composite.setLayout(new GridLayout(2, false));
		FormData fd_composite = new FormData();
		fd_composite.top = new FormAttachment(0, 10);
		fd_composite.left = new FormAttachment(0, 10);
		fd_composite.right = new FormAttachment(100, -10);
		composite.setLayoutData(fd_composite);
		
		Composite composite_2 = new Composite(shell, SWT.NONE);
		fd_composite.bottom = new FormAttachment(composite_2, -6);
		FormData fd_composite_2 = new FormData();
		fd_composite_2.top = new FormAttachment(0, 95);
		fd_composite_2.left = new FormAttachment(0, 10);
		fd_composite_2.right = new FormAttachment(100, -10);
		composite_2.setLayoutData(fd_composite_2);
		
		table = new Table(composite_2, SWT.BORDER | SWT.CHECK | SWT.FULL_SELECTION);
		table.setBounds(10, 10, 540, 229);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		
		TableColumn tblclmnLegajo = new TableColumn(table, SWT.NONE);
		tblclmnLegajo.setWidth(62);
		tblclmnLegajo.setText("Legajo");
		
		TableColumn tblclmnApellidoYNombre = new TableColumn(table, SWT.NONE);
		tblclmnApellidoYNombre.setWidth(280);
		tblclmnApellidoYNombre.setText("Apellido y Nombre");
		
		TableColumn tblclmnCuil = new TableColumn(table, SWT.NONE);
		tblclmnCuil.setWidth(130);
		tblclmnCuil.setText("CUIL");
		
		Composite composite_1 = new Composite(shell, SWT.NONE);
		fd_composite_2.bottom = new FormAttachment(100, -72);
		
		Label lblEmpresa = new Label(composite, SWT.NONE);
		lblEmpresa.setFont(SWTResourceManager.getFont("Calibri", 11, SWT.BOLD));
		GridData gd_lblEmpresa = new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1);
		gd_lblEmpresa.widthHint = 57;
		gd_lblEmpresa.heightHint = 19;
		lblEmpresa.setLayoutData(gd_lblEmpresa);
		lblEmpresa.setText("Empresa:");
		
		final Combo comboEmpresa = new Combo(composite, SWT.READ_ONLY);
		comboEmpresa.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				if (comboEmpresa.getSelectionIndex() > 0){
					table.removeAll();
					int id = Integer.parseInt(comboEmpresa.getData(comboEmpresa.getItem(comboEmpresa.getSelectionIndex())).toString());
					Empresa unaEmpresa = new Empresa().getDataDB(id);
					ObtenerDatos(unaEmpresa.getBase());
					
					//new renatre().main(empresa, fechainicio, bdatos, vector)
				
					
					
				}
			}
		});
		comboEmpresa.setVisibleItemCount(12);
		comboEmpresa.setItems(new String[] {"", "ALEJANDRO CAMERON S.A.", "AZTIRIA ELSA CLARA", "CLAVIER", 
				"CLAVIER BM", "ESTANCIA EL ALBARDON S.A.", "ISABEL DUGGAN DE DI CAMPELLO", "KENVIS S.A.", 
				"LOS HUAICOS M.L. S.A.", "TRES ISLAS S.A."});
		
		comboEmpresa.setData("ALEJANDRO CAMERON S.A.","57"); 
		comboEmpresa.setData("AZTIRIA ELSA CLARA","24"); 
		comboEmpresa.setData("CLAVIER","2"); 
		comboEmpresa.setData("CLAVIER BM","3"); 
		comboEmpresa.setData("ESTANCIA EL ALBARDON S.A.","5"); 
		comboEmpresa.setData("ISABEL DUGGAN DE DI CAMPELLO","6"); 
		comboEmpresa.setData("KENVIS S.A.","9"); 
		comboEmpresa.setData("LOS HUAICOS M.L. S.A.","13"); 
		comboEmpresa.setData("TRES ISLAS S.A.","11"); 
		
		comboEmpresa.select(0);

		comboEmpresa.setFont(SWTResourceManager.getFont("Calibri", 11, SWT.BOLD));
		GridData gd_comboEmpresa = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
		gd_comboEmpresa.widthHint = 251;
		gd_comboEmpresa.heightHint = 27;
		comboEmpresa.setLayoutData(gd_comboEmpresa);
		
		Label lblPeriodo = new Label(composite, SWT.NONE);
		lblPeriodo.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblPeriodo.setText("Periodo:");
		lblPeriodo.setFont(SWTResourceManager.getFont("Calibri", 11, SWT.BOLD));
		
		final Text dateTime = new Text(composite, SWT.BORDER | SWT.DROP_DOWN);
		dateTime.setToolTipText("Fecha Inicio: AAAA-MM-02");
		dateTime.setTextLimit(10);
		dateTime.setFont(SWTResourceManager.getFont("Calibri", 11, SWT.BOLD));
		GridData gd_dateTime = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_dateTime.heightHint = 20;
		gd_dateTime.widthHint = 87;
		dateTime.setLayoutData(gd_dateTime);
		FormData fd_composite_1 = new FormData();
		fd_composite_1.top = new FormAttachment(composite_2, 6);
		fd_composite_1.left = new FormAttachment(0, 10);
		fd_composite_1.right = new FormAttachment(100, -10);
		fd_composite_1.bottom = new FormAttachment(100, -10);
		composite_1.setLayoutData(fd_composite_1);
		
		Button btnAceptar = new Button(composite_1, SWT.NONE);
		btnAceptar.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
					
					int id = Integer.parseInt(comboEmpresa.getData(comboEmpresa.getItem(comboEmpresa.getSelectionIndex())).toString());
					
					ArrayList<Integer> empleado = new ArrayList<Integer>();

					
					for (int i = 0; i < table.getItemCount(); i++) {
						if ((table.getItem(i)).getChecked()) {
							empleado.add(Integer.valueOf(table.getItem(i).getText(0)));
						}
					}
					
					//for (int j = 0; j < empleado.size(); j++) {
						try {
							
							new renatre().writeCSV(id, dateTime.getText(), empleado);
						} catch (NullPointerException e2) {
							e2.printStackTrace();
						}
					//}

					//-------
			}
		});
		btnAceptar.setBounds(485, 20, 65, 25);
		btnAceptar.setText("Aceptar");
		
		Button btnLimpiar = new Button(composite_1, SWT.NONE);
		btnLimpiar.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				try {
					TableItem[]	item = table.getItems();
					for (int i = 0; i < table.getItemCount(); i++) {
						item[i].setChecked(false);
					}
				} catch (Exception e2) {
					e2.printStackTrace();
				}
			}
		});
		btnLimpiar.setText("Limpiar Seleccion");
		btnLimpiar.setBounds(10, 20, 105, 25);
		
		Button btnLimpiarTodo = new Button(composite_1, SWT.NONE);
		btnLimpiarTodo.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				MessageDialog dialog = new MessageDialog(shell, "Cupones", null,
						"El archivo de cupones fue generado exitosamente.", MessageDialog.CONFIRM, new String[] { "Aceptar",
				"Cancelar"}, 1);
				int result = dialog.open();
				if (result == 1){
					comboEmpresa.deselectAll();
					dateTime.setText("");
					table.removeAll();
				}

			}
		});
		btnLimpiarTodo.setText("Limpiar Todo");
		btnLimpiarTodo.setBounds(124, 20, 83, 25);
	}
}
